package com.u2b.business.status;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import orm.UpdateProxy;

import com.u2b.beans.Comment;
import com.u2b.beans.Status;
import com.u2b.business.location.DistanceService;
import com.u2b.business.net.FileUpload;
import com.u2b.business.time.TimeService;

import com.u2b.db.datasource.MyBeanUtils;
import com.u2b.db.status.StatusDaoImpl;
import com.u2b.global.utils.Constants;

public class StatusService
{
    private final StatusDaoImpl sdip;
    private final FileUpload fileUpload;
    private  final DistanceService  distanceService;
    private final  TimeService timeService;

    public StatusService()
    {
        sdip = new StatusDaoImpl();
        fileUpload = new FileUpload();
        distanceService=new DistanceService();
        timeService=new TimeService();

    }
    /**
     * 添加一个语音状态,由于使用了post方法,不得不传入一个HttpServletRequest对象
     * @param textParams
     * @param request
     * @return 返回一个primaryKey,大于0表示成功
     */
    public int addStatus_1(String[] textParams, HttpServletRequest request)
    {

        String[] urlNames = new String[]{ "photo_URL", "audio_URL" };
        String[] mimes = new String[]{ ".jpg", ".amr" };
        HashMap<String, Object> value = fileUpload.Upload(request, textParams, mimes, urlNames, "status");
        timeService.addCreateTime(value);
        distanceService.addGeoHash(value);
        int primaryKey = sdip.addStatus(value);
        return primaryKey;

    }

    public int addStatus_2(String[] textParams, HttpServletRequest request)
    {
        String[] urlNames = new String[]{ "photo_URL", "video_URL" };
        String[] mimes = new String[]{ ".jpg", ".3gp" };
        HashMap<String, Object> value = fileUpload.Upload(request, textParams, mimes, urlNames, "status");
        timeService.addCreateTime(value);
       distanceService.addGeoHash(value);
        int primaryKey = sdip.addStatus(value);
        return primaryKey;

    }

    public List<HashMap<String, Object>> getStatusList(HashMap<String, Object> initValues)
    {
       
        int page = Integer.valueOf(initValues.get("page").toString());
        if (page == 1)
        {
            distanceService.updateDistanceToKVDB(initValues); // 这个更新肯定是表示User的,也就是具体的User.注意要传一个uid
        }
        List<HashMap<String, Object>> result = sdip.getLastStatus(page);
        distanceService.addDistance(result, initValues);
        timeService.isExpire(result);

        return result;
    }

    public int commentThis(HashMap<String, Object> initValue)
    {

        timeService.addCreateTime(initValue);
        int flag = sdip.commentThis(initValue);
      
        return flag;
     

    }

    public List<HashMap<String, Object>> getDetailedMessage(int sid)
    {
        List<HashMap<String, Object>> result = sdip.getDetailedMessage(sid);
        return result;

    }
}
